<?php
namespace App\HttpController;


class Member extends \App\Base\AuthController
{

    public function __construct(){
        parent::__construct();
        $this->moduleid=2;
        $this->MD=$this->CORE_CONF->mod_single($this->moduleid);
        \copy_setting($this->moduleid);
        $this->setting=\load_setting($this->moduleid);
        $this->getView()->assign(['MODULEID'=>$this->moduleid,'MD'=>$this->MD]);
    }

    public function index()
    {
        $this->response()->write('member/index');
    }

    public function add(){
        $request=$this->request();
        $post = $request->getParsedBody();
        if($post){
            $post=\dtrim($post);
            $username=$post['username'];
            if(!$username){
                $this->msg(0,'账号不能为空');
                return;
            }
            $password=$post['password'];
            if(!$password){
                $this->msg(0,'密码不能为空');
                return;
            }
            $model = new \App\Model\MemberModel();
            $e = $model->where(['username'=>$username])->field('userid')->get();
            if($e){
                $this->msg(0,'账号已经存在');
                return;
            }

            $post['password']=md5($post['password']);

            $model = \App\Model\MemberModel::create($post);
            $insertid = $model->save();
            $this->msg($insertid);
            return;
        }
        $groupid=0;
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function lst(){
        $param = $this->request()->getRequestParam();
        extract($param);
        $model = new \App\Model\MemberModel();
        $where = [];
        isset($userid) or $userid='';
        if($userid){
            $where['userid']=$userid;
        }
        isset($username) or $username='';
        if($username){
            $where['username']=$username;
        }
        isset($groupid) or $groupid=0;
        if($groupid){
            $where['groupid']=$groupid;
        }
        isset($truename) or $truename='';
        if($truename){
            $where['truename']=["%{$truename}%",'like'];
        }
        $page = $param['page']??1;
        $pagesize = $this->setting['pagesize'];
        $data = $model->order('userid','desc')->limit(($page-1)*$pagesize,$pagesize)->withTotalCount()->select($where);
        $total = $model->lastQueryResult()->getTotalCount();
        $pager=\pagination($this->request()->getServerParams()['request_uri'],$param,$page,$pagesize,$total);
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function mod(){
        $get=$this->request()->getQueryParams();
        $userid=intval($get['userid']);
        if(!$userid){
            $this->msg(0);
            return;
        }
        $raw=$post=$this->request()->getParsedBody();
        if($post){
            unset($post['submit'],$post['FORWARD']);
            if(!$post['password']){
                unset($post['password']);
            }else{
                $post['password']=md5($post['password']);
            }
            $model = \App\Model\MemberModel::create()->get($userid);
            $r=$model->update($post);
            $this->msg($r,'',$raw['FORWARD']);
            return;
            //var_dump($model->lastQueryResult()->getLastError());
        }
        $model=new \App\Model\MemberModel();
        $data=$model->where(['userid'=>$userid])->get();
        if(!$data){
            $this->msg(0,'数据不存在');
            return;
        }

        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function del(){
        $get=$this->request()->getQueryParams();
        $userid=intval($get['userid']);
        if(!$userid){
            $this->msg(0);
            return;
        }
        $model = \App\Model\MemberModel::create()->get($userid);
        if(!$model){
            $this->msg(0,'数据不存在');
            return;
        }
        $r=$model->update(['groupid'=>3]);//$model->destroy();
        $this->msg($r);
        return;
    }

    public function outside(){
        $param = $this->request()->getRequestParam();
        extract($param);
        if(!isset($callback)){
            $this->msg(0,'lose callback');
            return;
        }
        $model = new \App\Model\MemberModel();
        $where = [];
        isset($userid) or $userid='';
        if($userid){
            $where['userid']=$userid;
        }
        isset($username) or $username='';
        if($username){
            $where['username']=$username;
        }
        isset($groupid) or $groupid=0;
        if($groupid){
            $where['groupid']=$groupid;
        }
        isset($truename) or $truename='';
        if($truename){
            $where['truename']=["%{$truename}%",'like'];
        }
        $page = $param['page']??1;
        $pagesize = $this->setting['pagesize'];
        $data = $model->order('userid','desc')->limit(($page-1)*$pagesize,$pagesize)->withTotalCount()->select($where);
        $total = $model->lastQueryResult()->getTotalCount();
        $pager=\pagination($this->request()->getServerParams()['request_uri'],$param,$page,$pagesize,$total);
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function login_log(){
        $param = $this->request()->getRequestParam();
        extract($param);
        $model = new \App\Model\Login_backModel();
    
        isset($username) or $username='';
        if($username){
            $model->where('username',$username);
        }
        isset($loginip) or $loginip='';
        if($loginip){
            $model->where('loginip',$loginip);
        }
        isset($logintime_1) or $logintime_1='';
        if($logintime_1){
            $model->where('logintime',strtotime($logintime_1),'>=');
        }
        isset($logintime_2) or $logintime_2='';
        if($logintime_2){
            $model->where('logintime',strtotime($logintime_2.' 23:59:59'),'<=');
        }
        $page = $param['page']??1;
        $pagesize = $this->setting['pagesize'];
        $data = $model->order('itemid','desc')->limit(($page-1)*$pagesize,$pagesize)->withTotalCount()->select();
        $total = $model->lastQueryResult()->getTotalCount();
        $pager=\pagination($this->request()->getServerParams()['request_uri'],$param,$page,$pagesize,$total);
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function setting(){
        $request=$this->request();
        $post = $request->getParsedBody();
        if($post){
            $r=\save_setting($this->moduleid,$post);
			$this->msg($r);
        }
        $setting=\load_setting($this->moduleid);
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function authenticate(){
        $get=$this->request()->getQueryParams();
        extract($get);
        isset($userid) or $userid=0;
		$userid=intval($userid);
		if(!$userid){
            $this->msg(0,'lose userid');
            return;
        }
		isset($moduleid) or $moduleid=1;
		$moduleid=intval($moduleid);
		
		$module_list=$this->CORE_CONF->MODULE;
		if(!array_key_exists($moduleid,$module_list)){
            $this->msg(0,'非法操作');
            return;
        }
		$model=new \App\Model\Access_backModel();
		
		$post=$this->request()->getParsedBody();
		if($post){
            $r=$model->set_auth($userid,$post);
            $this->msg($r);
            return;
		}
		
		$mod=$this->CORE_CONF->mod_single($moduleid);
		$checked=$model->get_auth($userid,$moduleid);

        $CORE_CONF=$this->CORE_CONF;
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

}